<?php

namespace app\api\controller;

use app\admin\model\Order;
use app\admin\model\UserRelation;
use app\common\controller\Api;
use app\service\DesEncrypt;
use app\service\FXApiService;
use think\facade\Log;
use think\facade\Queue;
/**
 *凤栖回调
 */
class FxCallBackNotify extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    public $JobClassName = 'app\job\EquityJob';
    /**
     * 接收订单数据
     *
     * @return void
     */
    public function OrderNotify()
    {
        $data = json_decode(file_get_contents('php://input'), true);
        Log::info("==凤栖接收订单数据==".json_encode($data));
        if($data['order']){
            $des = new DesEncrypt(config('apikey.FX_API.dec'));
            $post_data_str = $des->decrypt($data['order']);
            $list=json_decode($post_data_str,true);
            $orderModel=new Order();
            $orderFind=$orderModel->getFindOrderId($list['orderId'],1);
            if($orderFind){
                //修改
                $orderFind->orderStatus=$list['orderStatus'];
                $orderFind->updatetime=time();
                if($list['orderStatus']==2||$list['type']==6){
                    $JobQueueName = "";
                    $jobData = ['ts' => time(),'order_id'=>$orderFind->id];
                    Queue::later(10, $this->JobClassName, $jobData, $JobQueueName);
                    $orderFind->status=1;
                    // $l=FXApiService::queryProductCommissons($list['productId']);
                    // if($l['status']=='1'){
                    //     $orderFind->creat_date_end=time();
                    //     $orderFind->commission=$l['data']['CommissionsBean']['commission'];
                    // }
                }
                //退保
                if(in_array((int)$list['orderStatus'],[3,4,5,8,-9])||in_array($list['type'],[18,19])){
                    $orderFind->status=4;
                    $orderFind->refundTime=isset($list['updateTime'])?$list['updateTime']:date('Y-m-d H:i:s',time());
                }
                $orderFind->save();
                return true;
            }else{
                $UserRelationModel = new UserRelation();
                $user = $UserRelationModel->where('t_id', $list['mechanId'])->find();
                if ($user) {
                  $arr['user_id'] = $user->user_id;
                }
                //新增
                $arr=[
                    'ware_id'=>$list['productId'],//产品号id
                    'product_id'=>isset($list['productGradeId'])?$list['productGradeId']:'',//产品计划id
                    'ware_name'=>$list['productName'],//产品名称
                    'orderId'=>$list['orderId'],//订单号
                    'mechanId'=>$list['mechanId'],//渠道id  
                    'guaranteeTerm'=>$list['guaranteeTerm'],
                    'guaranteeType'=>$list['guaranteeType'],
                    'pay_freq'=>$list['payType'],
                    'payment'=>$list['payment'],
                    'insuredersJson'=>isset($list['insuredersJson'])?$list['insuredersJson']:'',
                    'benefitInfo'=>isset($list['benefitInfo'])?$list['benefitInfo']:'',
                    'payPerm'=>$list['payPerm'],
                    'premium'=>$list['premium'],
                    'totalPremium'=>$list['totalPremium'],
                    'totalInsured'=>$list['totalInsured'],
                    'expireTime'=>$list['expireTime'],
                    'orderStatus'=>$list['orderStatus'],
                    
                    'type'=>'1',
                    'status'=>'0',
                    'createtime'=>time()
                ];
                $ml=$orderModel->create($arr);
                if($ml){
                    return true;
                }
                return false;
            }
        }else{
            return false;
        }
    }
    /**
     * 保单回调
     *
     * @return void
     */
    public function policyOrderNotify(){
        $data = json_decode(file_get_contents('php://input'), true);
        Log::info("==凤栖保单回调==".json_encode($data));
        if($data['policy']){
            $des = new DesEncrypt(config('apikey.FX_API.dec'));
            $post_data_str = $des->decrypt($data['policy']);
            $list=json_decode($post_data_str,true);
            $orderModel=new Order();
            $orderFind=$orderModel->getFindOrderId($list['orderId'],1);

            if($orderFind){
                //修改
                $orderFind->orderStatus=$list['policyStatus'];
                $orderFind->effective_date=$list['waverLimit'];
                $orderFind->insure_data=$list['createTime'];
                $orderFind->updatetime=time();
                if($list['type']==10){
                    $orderFind->visitTime=$list['visitTime'];
                    $orderFind->returnVisitStatus=isset($list['returnVisitStatus'])?$list['returnVisitStatus']:'';
                }
                if($list['policyStatus']==1||$list['type']==1){
                    $orderFind->status=1;
                    $JobQueueName = "";
                    $jobData = ['ts' => time(),'order_id'=>$orderFind->id];
                    Queue::later(10, $this->JobClassName, $jobData, $JobQueueName);
                    // $l=FXApiService::queryProductCommissons($list['productId']);
                    // if($l['status']=='1'){
                    //     $orderFind->creat_date_end=time();
                    //     $orderFind->commission=$l['data']['CommissionsBean']['commission'];
                    // }
                }
                //退保
                if(in_array((int)$list['policyStatus'],[3,4,5])||in_array($list['type'],[12])){
                    $orderFind->status=4;
                    $orderFind->refundTime=$list['refundTime'];
                }
                $orderFind->save();
                return true;
            }else{
                $UserRelationModel = new UserRelation();
                $user = $UserRelationModel->where('t_id', $list['mechanId'])->find();
                if ($user) {
                  $arr['user_id'] = $user->user_id;
                }
                //新增
                $arr=[
                    'order_on'=>$list['orderNo'],//订单号
                    'policy_no'=>$list['policyNo'],//保单号
                    'ware_id'=>$list['productId'],//产品号id
                    'product_id'=>isset($list['productGradeId'])?$list['productGradeId']:'',
                    'ware_name'=>$list['productName'],
                    'orderId'=>$list['orderId'],
                    'mechanId'=>$list['mechanId'],
                    'guaranteeTerm'=>$list['guaranteeTerm'],
                    'guaranteeType'=>$list['guaranteeType'],
                    'pay_freq'=>$list['payType'],
                    'payment'=>$list['payment'],
                    'insuredersJson'=>isset($list['insuredersJson'])?$list['insuredersJson']:'',
                    'benefitInfo'=>isset($list['benefitInfo'])?$list['benefitInfo']:'',
                    'payPerm'=>$list['payPerm'],
                    'premium'=>$list['premium'],
                    'totalPremium'=>$list['totalPremium'],
                    'totalInsured'=>$list['totalInsured'],
                    'expireTime'=>$list['payTime'],
                    'orderStatus'=>$list['policyStatus'],
                    'expires'=>$list['guaranteeStartTime'].'-'.$list['guaranteeEndTime'],
                    'effective_date'=>$list['waverLimit'],//结算犹豫期
                    'insure_data'=>$list['createTime'],//承保时间
                    'returnVisitStatus'=>isset($list['returnVisitStatus'])?$list['returnVisitStatus']:'',
                    'type'=>'1',
                    'status'=>'0',
                    'createtime'=>time()
                ];
                $ml=$orderModel->create($arr);
                if($ml){
                    return true;
                }
                return false;
            }
        }else{
            return false;
        }
    }
}
